package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;

import javax.swing.*;
import java.io.File;

public class Controller {

    @FXML
    public Pane layoutPane;
    @FXML
    public MenuBar menuBar;
    @FXML
    public TextArea textArea;

    public File result;

    @FXML
    private void onMenuOpen(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        System.out.println(layoutPane);
        result = fileChooser.showOpenDialog(layoutPane.getScene().getWindow());
        if (result != null) {
            textArea.setText(FileTools.readFile(result));
        }
    }

    @FXML
    private void onMenuSave(ActionEvent event) {
        if(result != null){
            FileTools.writeFile(result, textArea.getText());
        }
    }

    @FXML
    private void onMenuClose(ActionEvent event) {
        System.exit(0);
    }

    @FXML
    private void onMenuDelete(ActionEvent event) {
        textArea.replaceSelection("");
    }

    @FXML
    private void onMenuAbout(ActionEvent event) {
        JOptionPane.showMessageDialog(null, "JavaFX记事本是一款使用JavaFX开发的记事本。" ,"关于",  JOptionPane.PLAIN_MESSAGE);
    }

    @FXML
    private void onContextSelectAll(ActionEvent event) {
        textArea.selectAll();
    }

}
